package com.omt.tarjimdinek.domain.islamicLibrary.entity;

import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;

import com.google.common.base.Objects;
import com.omt.tarjimdinek.support.Identifiable;

/**
 * Table A01_HDT
 * @author chabane
 *
 */
@Entity
@Table(name="a01_hdt")
public class Hadith implements Identifiable<Long>, Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(Tabihi.class);
    
	/** ID SGBD */
	@Column(name="CNHDT", length=11)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(name = "VERSION", precision = 10)
	@Version
	private Integer version;
	
	/** Number of hadith */
	@NotNull
	@Column(name="COHDT", nullable=false, length=11)
	private Integer numero;
	
	/** Content of hadith */
	@NotEmpty
	@Column(name="LIHDT", nullable=false)
	private String contenu;
	
	/** Determins if this hadith is authentics  */
	@NotNull
	@Column(name="BOAUTH", nullable=false, length=11)
	private Boolean authentic;
	
	@JoinTable(name = "a01_hdt_shb", 
			joinColumns = @JoinColumn(name = "CNHDT"), 
			inverseJoinColumns = @JoinColumn(name = "CNSHB"))
	@ManyToMany(cascade = { PERSIST, MERGE })
	private Set<Sahabi> listeSahabis;
	
	@JoinTable(name = "a01_hdt_tbh", 
			joinColumns = @JoinColumn(name = "CNHDT"), 
			inverseJoinColumns = @JoinColumn(name = "CNTBH"))
	@ManyToMany(cascade = { PERSIST, MERGE })
	private Set<Tabihi> listeTabihis;
	
	@JoinTable(name = "a01_hdt_imm", 
			joinColumns = @JoinColumn(name = "CNHDT"), 
			inverseJoinColumns = @JoinColumn(name = "CNIMM"))
	@ManyToMany(cascade = { PERSIST, MERGE })
	private Set<Imam> listeImam;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Transient
	public boolean isIdSet() {
	    return id != null && id.compareTo(Long.valueOf(0)) != 0;
	}

	/**
	 * @return the version
	 */
	public Integer getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * @return the numero
	 */
	public Integer getNumero() {
		return numero;
	}

	/**
	 * @param numero the numero to set
	 */
	public void setNumero(Integer numero) {
		this.numero = numero;
	}

	/**
	 * @return the contenu
	 */
	public String getContenu() {
		return contenu;
	}

	/**
	 * @param contenu the contenu to set
	 */
	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	/**
	 * @return the authentic
	 */
	public Boolean getAuthentic() {
		return authentic;
	}

	/**
	 * @param authentic the authentic to set
	 */
	public void setAuthentic(Boolean authentic) {
		this.authentic = authentic;
	}

	/**
	 * @return the listeSahabis
	 */
	public Set<Sahabi> getListeSahabis() {
		return listeSahabis;
	}

	/**
	 * @param listeSahabis the listeSahabis to set
	 */
	public void setListeSahabis(Set<Sahabi> listeSahabis) {
		this.listeSahabis = listeSahabis;
	}

	/**
	 * @return the listeTabihis
	 */
	public Set<Tabihi> getListeTabihis() {
		return listeTabihis;
	}

	/**
	 * @param listeTabihis the listeTabihis to set
	 */
	public void setListeTabihis(Set<Tabihi> listeTabihis) {
		this.listeTabihis = listeTabihis;
	}

	/**
	 * @return the listeImam
	 */
	public Set<Imam> getListeImam() {
		return listeImam;
	}

	/**
	 * @param listeImam the listeImam to set
	 */
	public void setListeImam(Set<Imam> listeImam) {
		this.listeImam = listeImam;
	}
	
    public int hashCode() {
        int hashCode = Objects.hashCode(getNumero());
        return hashCode;
    }
    
    /**
     * Construct a readable string representation for this Account instance.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return Objects.toStringHelper(this) //
                .add("id", getId()) //
                .add("numero", getNumero()) //
                .add("contenu", getContenu()) //
                .add("authentique", getAuthentic()) //
                .add("version", getVersion()) //
                .toString();
    }

}
